
#include "ftp/Utils.h"

#include <gtest/gtest.h>

TEST(FtpUtils, getCommandNameAndArgument_withWrongCommand)
{
	std::string cmdName;
	std::string argument;

	bool got = ftp::utils::getCommandNameAndArgument("PWD", &cmdName, &argument);
	EXPECT_FALSE(got); //there is no CRLF in the end of string
}

TEST(FtpUtils, getCommandNameAndArgument_withOnlyCommandName)
{
	std::string cmdName;
	std::string argument;

	bool got = ftp::utils::getCommandNameAndArgument("PWD\r\n", &cmdName, &argument);
	
	EXPECT_TRUE(got);
	EXPECT_EQ("PWD", cmdName);
	EXPECT_EQ("", argument);

	got = ftp::utils::getCommandNameAndArgument("CWD\r\n", &cmdName, &argument);

	EXPECT_TRUE(got);
	EXPECT_EQ("CWD", cmdName);
	EXPECT_EQ("", argument);
}

TEST(FtpUtils, getCommandNameAndArgument_withArguments)
{
	std::string cmdName;
	std::string argument;

	bool got = ftp::utils::getCommandNameAndArgument("PASS password\r\n", &cmdName, &argument);
	
	EXPECT_TRUE(got);
	EXPECT_EQ("PASS", cmdName);
	EXPECT_EQ("password", argument);

	got = ftp::utils::getCommandNameAndArgument("USER user\r\n", &cmdName, &argument);

	EXPECT_TRUE(got);
	EXPECT_EQ("USER", cmdName);
	EXPECT_EQ("user", argument);
}

TEST(FtpUtils, makeCommonResponse)
{
	std::string response = ftp::utils::makeCommonResponse(200, "OK");
	EXPECT_EQ("200 OK\r\n", response);

	response = ftp::utils::makeCommonResponse(111, "Not found");
	EXPECT_EQ("111 Not found\r\n", response);
}

TEST(FtpUtils, makePwdResponse)
{
	std::string response = ftp::utils::makePwdResponse(257, "/" , "directory printed");
	EXPECT_EQ("257 \"/\" directory printed\r\n", response);

	response = ftp::utils::makePwdResponse(111, "/test/test" , "directory printed");
	EXPECT_EQ("111 \"/test/test\" directory printed\r\n", response);
}

TEST(FtpUtils, makePasvUtils)
{
	std::string response = ftp::utils::makePasvResponse(227, "127.0.0.1", 50001, "Ok");

	EXPECT_EQ("227 127,0,0,1,195,21 Ok\r\n", response);
}
